//
//  DoctorsSeriesViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-12.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "DoctorsSeriesViewController.h"
#import "DoctorInfoViewController.h"

static ASIHTTPConnection *doctors_connect;

@interface DoctorsSeriesViewController ()

@end



@implementation DoctorsSeriesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)dealloc{
    
    if (doctors_connect != nil) {
        doctors_connect.connectionDelegate = nil;
        [doctors_connect release];
        doctors_connect = nil;
    }
    
    [myArray release];
    [myTableView release];
    
    [super dealloc];
}


- (id)initWithSectionId:(NSString *)section contentType:(int)type{
    self = [super init];
    if (self) {
        
        contentType = type;
        sectionId =section;
        
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    myArray = [[NSMutableArray alloc]init];
    
    [self loadDoctors];
    
}

- (void)loadDoctors{
    
    NSString *urlString ;
    
    if (contentType == 1) {
        
        urlString   = [NSString stringWithFormat:@"%@/selectDepartment?lab_id=%@",UCENTERBASEURL,sectionId];
      

    }
    else{
        
        urlString   = [NSString stringWithFormat:@"%@/selectAppointDoctor?Lab_ID=%@",UCENTERBASEURL,sectionId];
               
    }
    
    NSURL *url = [NSURL URLWithString:urlString];

      
    if (doctors_connect != nil) {
        doctors_connect.connectionDelegate = nil;
        [doctors_connect release];
        doctors_connect = nil;
    }
    
    doctors_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    doctors_connect.connectionDelegate = self;
    
    [doctors_connect startAsynchronous];
    
}

#pragma mark connectino delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    NSArray *array = [dict objectForKey:@"doctorlist"];
    
    if (myArray == nil) {
        myArray = [[NSMutableArray alloc]init];
    }
    
    if (myArray != nil) {
        [myArray removeAllObjects];
    }
    
    [myArray addObjectsFromArray:array];
    [myTableView reloadData];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"creditCell";
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 200, 40)];
        [titleLable setTextAlignment:NSTextAlignmentLeft];
        [titleLable setTextColor:[UIColor blackColor]];
        [titleLable setFont:[UIFont boldSystemFontOfSize:17]];
        titleLable.backgroundColor = [UIColor clearColor];
    
        titleLable.tag = 100;
        [cell addSubview:titleLable];
     
        [titleLable release];
        
        UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 79, 89)];
        iconImageView.image = [UIImage imageNamed:@"doctorPhoto.jpg"];
        [cell addSubview:iconImageView];
        iconImageView.tag = 101;
        [iconImageView release];
        
        UILabel *birthDayLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 200, 20)];
        birthDayLable.textColor = [UIColor blackColor];
        birthDayLable.backgroundColor = [UIColor clearColor];
        [birthDayLable setFont:[UIFont systemFontOfSize:14]];
        [birthDayLable setTextAlignment:UITextAlignmentLeft];
        birthDayLable.tag = 102;
        [cell addSubview:birthDayLable];
        [birthDayLable release];
        
        UILabel *sexLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 70, 200, 20)];
        sexLable.textColor = [UIColor blackColor];
        sexLable.backgroundColor = [UIColor clearColor];
        [sexLable setTextAlignment:UITextAlignmentLeft];
        [sexLable setFont:[UIFont systemFontOfSize:14]];
        sexLable.tag = 103
        ;
        [cell addSubview:sexLable];
        [sexLable release];
    }
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    
    UILabel *lable = (UILabel *)[cell viewWithTag:100];
    
    
    UILabel *birthLable = (UILabel *)[cell viewWithTag:102];
   
    
    UILabel *sexLable = (UILabel *)[cell viewWithTag:103];
  
    
    if (contentType == 1) {
        
        lable.text = [dict objectForKey:@"doctorName"];
         birthLable.text = [NSString stringWithFormat:@"生日：%@",[dict objectForKey:@"birthday"]];
          sexLable.text = [NSString stringWithFormat:@"性别：%@",[dict objectForKey:@"doctor_Sex"]];
    }
    else{
        
        lable.text = [dict objectForKey:@"doctro_Name"];
        birthLable.text = [NSString stringWithFormat:@"值班时间：%@",[dict objectForKey:@"duty_Day"]];
        sexLable.text = [NSString stringWithFormat:@"值班类型：%@",[dict objectForKey:@"duty_Type"]];

    }
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
   
    if (contentType == 1) {
        
        NSString *doctorId = [dict objectForKey:@"doctor_ID"];
        
        DoctorInfoViewController *viewController = [[DoctorInfoViewController alloc] initWithDoctorId:doctorId];
        
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }
    else{
        
        NSString *doctorId = [dict objectForKey:@"duty_ID"];
        
        BookDetailViewController *viewController = [[BookDetailViewController alloc] initWithDutyId:doctorId];
        
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }

    
    
}


@end
